package com.ruoyi.web.controller.demo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.DemoStudent;
import com.ruoyi.demo.service.base.DemoStudentService;
import com.ruoyi.framework.web.service.TokenService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;


/**
 * 学生信息Controller
 * 
 * @author zhangshu
 * @date 2021-01-25
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/demo/student" )
public class DemoStudentController extends BaseController {

    private final DemoStudentService iDemoStudentService;
    private final TokenService tokenService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('demo:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(DemoStudent demoStudent) {
        //使用分页
        startPage();
        //List<DemoStudent> list = iDemoStudentService.queryList(demoStudent);
        List<DemoStudent> list = iDemoStudentService.findByAll(demoStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('demo:student:export')" )
    @Log(title = "学生信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(DemoStudent demoStudent) {
        List<DemoStudent> list = iDemoStudentService.queryList(demoStudent);
        ExcelUtil<DemoStudent> util = new ExcelUtil<DemoStudent>(DemoStudent.class);
        return util.exportExcel(list, "student" );
    }

    /**
     * 导入学生信息列表
     */
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('demo:student:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<DemoStudent> util = new ExcelUtil<DemoStudent>(DemoStudent.class);
        List<DemoStudent> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = iDemoStudentService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<DemoStudent> util = new ExcelUtil<DemoStudent>(DemoStudent.class);
        return util.importTemplateExcel("学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('demo:student:query')" )
    @GetMapping(value = "/{studentId}" )
    public AjaxResult getInfo(@PathVariable("studentId" ) Long studentId) {
        return AjaxResult.success(iDemoStudentService.getById(studentId));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('demo:student:add')" )
    @Log(title = "学生信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemoStudent demoStudent) {
        return toAjax(iDemoStudentService.save(demoStudent) ? 1 : 0);
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('demo:student:edit')" )
    @Log(title = "学生信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemoStudent demoStudent) {
        return toAjax(iDemoStudentService.updateById(demoStudent) ? 1 : 0);
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('demo:student:remove')" )
    @Log(title = "学生信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentIds}" )
    public AjaxResult remove(@PathVariable Long[] studentIds) {
        return toAjax(iDemoStudentService.removeByIds(Arrays.asList(studentIds)) ? 1 : 0);
    }
}
